package panyy.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import panyy.domain.User;
import panyy.service.UserService;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping("/select/{id}")
    public User findById(@PathVariable int id){
        return userService.findById(id);
    }

    @RequestMapping("/insert")
    public int insert(@RequestBody String json){
        JSONObject obj = JSON.parseObject(json);
        User user = new User(obj.getString("name"),
                obj.getInteger("age"));
        return userService.insert(user);
    }
}
